package com.baike.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.MongodbComment;
import com.baike.service.MongodbService;
import com.baike.util.IdWorker;
import com.baike.vo.MongodbVO;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author:pjm
 */
@RestController
@RequestMapping("mongodbController")
@Api(value = "学生评论服务管理")
@RefreshScope //开启自动刷新配置
public class MongodbController {
    @Autowired
    private MongodbService mongodbService;
    @Autowired
    private MongoTemplate mongoTemplate;

    @ApiOperation(value = "评论增加接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mongodbComment",value = "评论实体类",required = true,paramType = "body")
    })
    @PostMapping("/add")
    public Result addComment(@RequestBody MongodbComment mongodbComment){

        IdWorker idWorker = new IdWorker();
        mongodbComment.setCommentId(idWorker.nextId());//雪花id
        mongodbComment.setCommentDatetime(new Date());
        mongodbComment.setPrivates(1); //默认为公开的
        MongodbComment comment = mongodbService.addComment(mongodbComment);
        return Result.build(ConstantEnum.INSERT_SUCCESS).setResult(comment);
    }

    //根据课程查询评论,默认为公开的评论
    @ApiOperation(value = "评论id查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId",value = "评论id",required = true,paramType = "path")
    })
    @GetMapping("/{commentId}")
    public Result selectComment(@PathVariable("commentId")Long commentId){
        MongodbComment mongodbComment = mongodbService.selectById(commentId);
        return Result.build(ConstantEnum.SUCCESS).setResult(mongodbComment);
    }

    //根据条件查询
    @ApiOperation(value = "评论条件查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lessonId",value = "课程id",required = true,paramType = "path")
    })
    @GetMapping("selectById/{lessonId}")
    public Result selectById(@PathVariable("lessonId")Integer lessonId){
        Query query = new Query();
        query.addCriteria(Criteria.where("lessonId").is(lessonId));
        List<MongodbComment> mongodbComments = mongoTemplate.find(query, MongodbComment.class);
        return Result.build(ConstantEnum.SUCCESS).setResult(mongodbComments);
    }

    //根据评论内容分页加模糊查询
    @ApiOperation(value = "评论分页加模糊查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentContent",value = "评论内容",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNum",value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType = "query"),
    })
    @GetMapping("findPage")
    public Result findPage(@RequestParam(defaultValue = "commentContent") String commentContent,
                             @RequestParam Integer pageNum,
                             @RequestParam Integer pageSize){
        long count = mongoTemplate.count(new Query(), MongodbComment.class);
        System.out.println("总条数"+count);
        Pattern pattern = Pattern.compile("^.*" + commentContent + ".*$",Pattern.CASE_INSENSITIVE);
        Query query = new Query();
        query.addCriteria(Criteria.where("commentContent").regex(pattern));
        query.skip((pageNum-1)*pageSize).limit(pageSize);
        List<MongodbComment> commentList = mongoTemplate.find(query, MongodbComment.class);
        return Result.build(ConstantEnum.SUCCESS).setResult(commentList);
    }

    //非覆盖修改
    @ApiOperation(value = "评论内容修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId",value = "评论人id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "commentContent",value = "评论内容",required = true,paramType = "query")
    })
    @PutMapping("/{studentId}")
    public Result updateById(@RequestParam(defaultValue = "studentId")Integer studentId,
                             @RequestParam(defaultValue = "commentContent")String commentContent){
        //先查询出mongodb数据库文档的数据
        Query query = new Query();
        query.addCriteria(Criteria.where("studentId").is(studentId));

        //构造器中填写需要修改的字段和值
        Update update = new Update().set("commentContent",commentContent);

        //返回修改后的第一条数据
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, MongodbComment.class);
        //获取匹配的数量
        System.out.println(updateResult.getMatchedCount());
        //获取修改的数量
        System.out.println(updateResult.getModifiedCount());
        //返回修改的所有数据,批量修改
        //UpdateResult result = mongoTemplate.updateMulti(query, update, MongodbComment.class);
        return Result.build(ConstantEnum.UPDATE_SUCCESS).setResult(updateResult);

    }

    //删除评论信息
    @ApiOperation(value = "评论信息删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId",value = "commentId",required = true,paramType = "query")
    })
    @DeleteMapping
    public Result deleteById(@RequestParam(defaultValue = "commentId")Long commentId){
        MongodbComment comment = new MongodbComment();
        comment.setCommentId(commentId);
        mongoTemplate.remove(comment);
        return Result.build(ConstantEnum.SUCCESS);
    }

}
